﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QFlow.Utils;
using QFlow.Data;

namespace QFlow.Windows.Reports
{
    public partial class ReturnsReportViewer : BaseForm
    {
        public ReturnsReportViewer(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
        }

        private void ReturnsReportViewer_Load(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int returnedItemTypeId = this.Context.ItemTypes.GetReturnedType().ItemTypeID;
            int closedStatusId = this.Context.OrderStatus.GetClosedStatus().StatusID;

            int type = (cmbType.SelectedIndex == -1 ? -1 : cmbType.SelectedIndex);

            DateTime fromDate = dtFrom.LockUpdateChecked ? dtFrom.Value.Date : DateTime.MinValue;
            DateTime toDate = dtTo.LockUpdateChecked ? dtTo.Value.Date : DateTime.MinValue;

            IEnumerable<ReturnsReportResult> purchased = null;
            if (type == -1 || type == 0)
            {
                purchased = from pOrder in this.Context.PurchaseOrders
                            join pItem in this.Context.PurchaseItems
                            on pOrder.PurchaseOrderID equals pItem.PurchaseOrderID

                            where
                            pOrder.StatusID == closedStatusId
                            &&
                            pItem.ItemTypeID == returnedItemTypeId
                            &&
                            (!dtFrom.LockUpdateChecked ? true : pOrder.Date >= fromDate)
                            &&
                            (!dtTo.LockUpdateChecked ? true : pOrder.Date <= toDate)

                            select new ReturnsReportResult
                            {
                                OrderID = pOrder.PurchaseOrderID,
                                OperationType = QFlow.Windows.Properties.Resources.RPPurchased,
                                Product = pItem.Product.Name,
                                Quantity = pItem.Quantity,
                                OriginalPrice = pItem.PurchaseItem1.UnitPrice,
                                ReturnPrice = pItem.UnitPrice
                            };
            }

            IEnumerable<ReturnsReportResult> sold = null;
            if (type == -1 || type == 1)
            {
                sold = from sOrder in this.Context.SaleOrders
                       join sItem in this.Context.SaleItems
                       on sOrder.SaleOrderID equals sItem.SalesOrderID

                       where
                       sOrder.StatusID == closedStatusId
                       &&
                       sItem.ItemTypeID == returnedItemTypeId
                       &&
                       (!dtFrom.LockUpdateChecked ? true : sOrder.Date.Date >= fromDate)
                       &&
                       (!dtTo.LockUpdateChecked ? true : sOrder.Date.Date <= toDate)

                       select new ReturnsReportResult
                       {
                           OrderID = sOrder.SaleOrderID,
                           OperationType = QFlow.Windows.Properties.Resources.RPSales,
                           Product = sItem.StockableProduct.Product.Name,
                           Quantity = sItem.Quantity,
                           OriginalPrice = sItem.SaleItem1.UnitPrice,
                           ReturnPrice = sItem.UnitPrice
                       };
            }

            List<Microsoft.Reporting.WinForms.ReportParameter> repeortParams = new List<Microsoft.Reporting.WinForms.ReportParameter>();
            IEnumerable<ReturnsReportResult> result;
            if (type == -1)
            {
                result = purchased.Union(sold).OrderBy(detail => detail.OrderID);
                repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("Type", "All"));
            }
            else if (type == 0)
            {
                result = purchased.OrderBy(detail => detail.OrderID);
                repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("Type", QFlow.Windows.Properties.Resources.RPPurchased));
            }
            else
            {
                result = sold.OrderBy(detail => detail.OrderID);
                repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("Type", QFlow.Windows.Properties.Resources.RPSales));
            }

            ReturnsReportResultBindingSource.DataSource = result.ToArray();

            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("FromDate", (dtFrom.LockUpdateChecked ? fromDate.ToString("dd-MM-yyyy") : "All")));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("ToDate", (dtTo.LockUpdateChecked ? toDate.ToString("dd-MM-yyyy") : "All")));

            reportViewer1.LocalReport.SetParameters(repeortParams);

            reportViewer1.RefreshReport();
        }
    }

    public class ReturnsReportResult
    {
        public int OrderID { get; set; }
        public string Product { get; set; }
        public string OperationType { get; set; }
        public double Quantity { get; set; }
        public double OriginalPrice { get; set; }
        public double ReturnPrice { get; set; }
    }
}
